<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="70px">
      <el-row :gutter="24">
        <el-col :span="4">
          <el-form-item label="名称" prop="nickName">
            <el-input v-model="form.name" placeholder="请输入优惠劵名称" />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-button type="primary" @click="loadData">搜索</el-button>
          <el-button @click="searchResetForm('form')">重 置</el-button>

        </el-col>
      </el-row>
    </el-form>

    <div style="margin-bottom: 20px;">
      <el-button type="primary" @click="add">添加</el-button>
    </div>
    <el-table
      :loading="loading"
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="50"
      />
      <el-table-column v-for="(item,index) in columnLists" :key="index" :prop="item.prop" :label="item.label" align="center" />

      <el-table-column
        fixed="right"
        label="操作"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            type="text"

            @click="edit(scope.$index)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <Pagination :total="total" :current-page="pageNum" :page-size="pageSize" @handleChange="handleChange" />

    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogVisible" width="45%">
      <el-form ref="formLabelAlign" :model="formLabelAlign" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="10">
            <el-form-item label="抢单数量" prop="orderNum">
              <el-input v-model="formLabelAlign.orderNum" clearable />
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item label="抢单金额" prop="orderPrice">
              <el-input v-model="formLabelAlign.orderPrice" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="优惠劵名称" prop="name">
              <el-input v-model="formLabelAlign.name" clearable />
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item label="优惠劵金额" prop="couponsPrice">
              <el-input v-model="formLabelAlign.couponsPrice" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="save('formLabelAlign')">确 定</el-button>
        <el-button size="mini" @click="resetForm('formLabelAlign')">重 置</el-button>
        <el-button size="mini" type="danger" @click="cancel('formLabelAlign')">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { showData, resetForm, validate, success, fail } from '@/api/common'
import Pagination from '@/components/Pagination'
import Column from '@/components/Column'
import { get, lists, post, del } from '@/api/requestCommon'
export default {
  name: 'PagePermission',
  components: {
    Pagination,
    Column
  },
  data() {
    return {
      imageData: [],
      pageNum: 1, pageSize: 10, total: 0,
      form: { nickName: '', phone: '' },
      tableData: [],
      selectRows: [],
      dialogVisible: false,
      title: '添加优惠券',
      editId: '',
      columnLists: [
        { prop: 'name', label: '优惠券名称' },
        { prop: 'couponsPrice', label: '优惠券金额' },
        { prop: 'orderNum', label: '抢单数量' },
        { prop: 'orderPrice', label: '抢单金额' },

        { prop: 'createTime', label: '创建时间' }
      ],
      type: 'coupons',

      rules: {
        name: [
          { required: true, message: '请输入优惠劵名称', trigger: 'change' }
        ],
        orderNum: [
          { required: true, message: '请输入订单数量', trigger: 'change' }
        ],
        orderPrice: [
          { required: true, message: '请输入订单金额', trigger: 'change' }
        ],
        couponsPrice: [
          { required: true, message: '请输入优惠券金额', trigger: 'change' }
        ]
      },
      formLabelAlign: {
        id: '',
        name: '',
        couponsPrice: '',
        orderPrice: '',
        orderNum: ''
      },
      loading: true

    }
  },
  created() {

  },
  mounted() {
    this.loadData()
  },
  methods: {
    add() {
      this.dialogVisible = true
    },

    edit(index) {
      this.title = '编辑优惠券'
      this.dialogVisible = true
      const row = this.tableData[index]
      get('coupons/findById/' + row.id).then(response => {
        if (response.code === 200) {
          debugger
          Object.assign(this.formLabelAlign, response.data)
          this.imageUrl = response.data.image
        }
      })
    },
    cancel(formName) {
      resetForm(this, formName)
      this.dialogVisible = false
    },

    resetForm(formName) {
      resetForm(this, formName)
    },
    searchResetForm(formName) {
      resetForm(this, formName)
      this.loadData()
    },

    selectionChange(rows) {
      this.selectRows = rows
    },

    handleChange(pageNum, pageSize) {
      this.pageNum = pageNum
      this.pageSize = pageSize
      this.loadData()
    },

    loadData() {
      this.form.pageNum = this.pageNum
      this.form.pageSize = this.pageSize
      lists('coupons', this.form).then(response => {
        if (response.code === 200) {
          response.data.records.forEach(item => {
            this.imageData.push(item.image)
          })
          showData(response, this)
        }
      })
    },
    save(formName) {
      if (validate(this, formName)) {
        post('coupons/save', this.formLabelAlign).then(response => {
          if (response.code === 200) {
            success(response.msg)
            this.dialogVisible = false
            this.loadData()
          } else {
            fail(response.msg)
          }
        })
      }
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
